// 工单/yt-order/yt-order-server/src/main/java/com/yt/order/service/StatisticsServiceImpl.java
package com.yt.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yt.order.models.service.IStatisticsService;
import com.yt.order.models.service.IWorkOrderInfoService;
import com.yt.order.models.service.ICustomerInfoService;
import com.yt.order.models.dto.WorkOrderInfoDTO;
import com.yt.order.models.dto.CustomerInfoDTO;
import com.yt.order.models.vo.StatisticsVO;
import java.util.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.LocalTime;

/**
 * 统计服务实现类
 *
 * @author xieyanfei
 * @Date 2025-09-05 15:40:00
 */
@DubboService
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    private IWorkOrderInfoService workOrderInfoService;

    @Autowired
    private ICustomerInfoService customerInfoService;

    @Override
    public StatisticsVO getHomePageStatistics() {
        StatisticsVO statisticsVO = new StatisticsVO();

        // 1. 统计工单总数
        int totalWorkOrders = workOrderInfoService.selectCount(null);
        statisticsVO.setTotalWorkOrders(totalWorkOrders);

        // 2. 统计今日新增工单
        Wrapper<WorkOrderInfoDTO> workOrderWrapper = new EntityWrapper<>();
        // 获取今日开始时间和结束时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        workOrderWrapper.ge("create_time", Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant()));
        workOrderWrapper.le("create_time", Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()));

        int todayNewWorkOrders = workOrderInfoService.selectCount(workOrderWrapper);
        statisticsVO.setTodayNewWorkOrders(todayNewWorkOrders);

        // 3. 统计客户总数
        Wrapper<CustomerInfoDTO> customerWrapper = new EntityWrapper<>();
        // 排除已删除的客户
        customerWrapper.eq("is_del", 0);

        int totalCustomers = customerInfoService.selectCount(customerWrapper);
        statisticsVO.setTotalCustomers(totalCustomers);

        return statisticsVO;
    }


    // 工单总数
    @Override
    public int getTotalWorkOrders() {
        return workOrderInfoService.selectCount(null);
    }

    // 今日新增工单
    @Override
    public int getTodayNewWorkOrders() {
        Wrapper<WorkOrderInfoDTO> workOrderWrapper = new EntityWrapper<>();
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        workOrderWrapper.ge("create_time", Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant()));
        workOrderWrapper.le("create_time", Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()));

        return workOrderInfoService.selectCount(workOrderWrapper);
    }

    // 客户总数
    @Override
    public int getTotalCustomers() {
        Wrapper<CustomerInfoDTO> customerWrapper = new EntityWrapper<>();
        customerWrapper.eq("is_del", 0); // 排除已删除的客户
        return customerInfoService.selectCount(customerWrapper);
    }
}